using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Heal200 : SkillBasic
{
    public float CD;
    private float CDtime;
    PlayerBasic player;
    ParticleSystem particle;
    public float skilltime;
    public float damagereduction;
    public float recoverry;
    public float Bigrecoverry;
    bool isskilling;
    TextMeshProUGUI text;
    public GameObject healtimes;
    int skilltimes;
    CircleCollider2D collider;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponentInParent<PlayerBasic>();
        particle = GetComponentInChildren<ParticleSystem>();
        particle.Stop();
        Instantiate(healtimes, GameObject.Find("heal(Clone)").transform);
        text = GameObject.Find("healtimes(Clone)").GetComponent<TextMeshProUGUI>();
        collider = GetComponent<CircleCollider2D>();
        HealCD.CD = CD;
        HealCD.CDCurrent = 0;
        isskilling = false;
        particle.Stop();
    }
    void skillend()
    {
        player.damagereduction = 0;
        particle.Stop();
        isskilling = false;
        collider.enabled = false;
    }
    public IEnumerator Skilling()
    {
        yield return new WaitForSeconds(skilltime);
        skillend();

    }
    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Creation"))
        {

            other.GetComponent<Creation>().health+=Bigrecoverry*0.5f*player.health;
        }
    }
    void Skill()
    {
        player.Recovery(Bigrecoverry * player.health);
        isskilling = true;
        player.damagereduction = damagereduction;
        particle.Play();
        collider.enabled = true;
        StartCoroutine(Skilling());
    }
    void Update()
    {
        if (CDtime > 0)
        {
            CDtime -= Time.deltaTime;
            HealCD.CDCurrent = CDtime;
        }
        else
        {
            if (skilltimes < 2)
            {
                skilltimes++;
                if (skilltimes < 2)
                    CDtime = CD;

            }
        }
        text.text = skilltimes.ToString();
        if (Input.GetKeyDown(KeyCode) && Time.timeScale > 0)
        {
            if (skilltimes > 0 && (!isskilling))
            {
                skilltimes--;
                Skill();
                if (skilltimes == 1)
                {
                    CDtime = CD;
                }
            }


        }
        if (isskilling)
        {
            player.Recovery(recoverry * player.health);
        }
    }
}
